package loveqq.niceexcel.converter.internal.number;

import loveqq.niceexcel.annotation.internal.Internal;
import loveqq.niceexcel.converter.CellData;
import loveqq.niceexcel.converter.ConvertContext;
import loveqq.niceexcel.converter.Converter;
import loveqq.niceexcel.util.NumberUtils;
import org.apache.poi.ss.usermodel.CellType;

import java.math.BigDecimal;
import java.text.ParseException;

/**
 * @author zhongjh@tsintergy.com
 * @date 2022-08-19
 */
@Internal
public class DoubleNumericConverter implements Converter<Double> {
    @Override
    public Class<Double> supportJavaType() {
        return Double.class;
    }

    @Override
    public CellType supportExcelType() {
        return CellType.NUMERIC;
    }

    @Override
    public Double convertToJavaData(CellData<?> cellData, ConvertContext context) {
        BigDecimal value = cellData.getNumberValue();
        return (value == null) ? null : value.doubleValue();
    }

    @Override
    public CellData<?> convertToExcelData(Double data, ConvertContext context) throws ParseException {
        BigDecimal value = (data == null) ? null : NumberUtils.parseBigDecimal(data.toString(), context.getNumberFormat());
        return new CellData<>(value);
    }
}
